package com.erp.module.caiLiaoZhiLiang

import android.app.Activity
import android.content.Intent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import com.erp.R
import com.erp.bean.CaiLiaoZhiLiangBean
import com.erp.mclass.QuanXian
import com.erp.mclass.UnCeHandler
import com.erp.module.caiLiaoZhiLiang.xGCaiLiaoZhiLiang.CaiLiaoZhiLiangActivity
import com.erp.myAdapter.ArrayAdapter
import com.erp.myAdapter.CaiLiaoZhiLiangKAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.util.Dialogutil
import com.erp.util.Myutil
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragment_cailiao_zhiliang.*


/**
 * Created by Administrator on 2018/5/14/014.
 * 作者：刘洋
 * 材料质量
 */
class FragmentKCaiLiaoZhiLiang : BaseKFragment(), PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener, AdapterView.OnItemClickListener, CaiLiaoZhiLiangContract.CaiLiaoZhiLiangKView, AdapterView.OnItemLongClickListener, View.OnClickListener {


    val f = "FragmentKCaiLiaoZhiLiang"
    lateinit var cailiaozhiliangs: MutableList<CaiLiaoZhiLiangBean>
    lateinit var KP: CaiLiaoZhiLiangContract.CaiLiaoZhiLiangKP
    var updatedialog: AlertDialog? = null
    lateinit var adapter: CaiLiaoZhiLiangKAdapter
    override fun getLayoutId(): Int {
        fragmentname = f
        return R.layout.fragment_cailiao_zhiliang
    }

    override fun initData() {
        UnCeHandler.buzhou = "initData"
        cailiaozhiliangs = ArrayList<CaiLiaoZhiLiangBean>()
        adapter = CaiLiaoZhiLiangKAdapter(context!!, cailiaozhiliangs)
        KP = ICaiLiaoZhiLiangKP(cailiaozhiliangs, this, context!!)
        initView()
//        updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
//        KP.sousuo("","","","")
        Pull.headerRefreshing()
//        KP.xiala()
    }

    fun qingkong(){
        if(isFirstLoad){
            return
        }
        Pull.headerRefreshing()
    }


    private fun initView() {
        UnCeHandler.buzhou = "initView"
        Pull.setheader(true)
        Pull.setOnFooterRefreshListener(this)
        Pull.setOnHeaderRefreshListener(this)
        LlXingJian.setOnClickListener(this)
        LvCaiLiaoZhiLiang.onItemClickListener = this
        LvCaiLiaoZhiLiang.onItemLongClickListener = this
        TvStartTime.setOnClickListener(this)
        TvEndTime.setOnClickListener(this)
        TvStartTime.text = Myutil.getjulidate(-1)
        TvEndTime.text = Myutil.getjulidate(0)
        EtSouSuo.setOnEditorActionListener { v, actionId, event ->
            var isOK = true
            when (actionId) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                    KP.sousuo(EtSouSuo.text.toString(), SpHeGe.selectedItem.toString(), TvStartTime.text.toString(), TvEndTime.text.toString())
                }
                else -> isOK = false
            }
            isOK
        }
        BtGuolv.setOnClickListener(this)
        TvChongzhi.setOnClickListener(this)
        LvCaiLiaoZhiLiang.adapter = adapter
        val listsp = ArrayList<String>()
        listsp.add("全部")
        listsp.add("合格")
        listsp.add("不合格")
        val adapter = ArrayAdapter(activity, R.layout.lv_sp_tvitem, listsp)
        SpHeGe.adapter = adapter
    }

    override fun onClick(v: View?) {
        when (v) {
            BtGuolv -> {
                updatedialog = Myutil.showupdatedialog(activity, "正在过滤", false)
                KP.sousuo(EtSouSuo.text.toString(), SpHeGe.selectedItem.toString(), TvStartTime.text.toString(), TvEndTime.text.toString())
            }
            TvChongzhi -> {
                TvStartTime.text = ""
                TvEndTime.text = ""
                SpHeGe.setSelection(0)
                EtSouSuo.setText("")
                updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                KP.sousuo(EtSouSuo.text.toString(), SpHeGe.selectedItem.toString(), TvStartTime.text.toString(), TvEndTime.text.toString())
            }
            TvStartTime -> {
                Dialogutil.showBottoPopupWindow(TvStartTime, "选择开始时间", TvStartTime.text.toString(), activity)
            }
            TvEndTime -> {
                Dialogutil.showBottoPopupWindow(TvEndTime, "选择开始时间", TvEndTime.text.toString(), activity)
            }
            LlXingJian -> {
                xingjian()
            }
        }
    }

    //点击一条
    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        UnCeHandler.buzhou = "onItemClick"
        it = cailiaozhiliangs[position]
        val intent = Intent(context, CaiLiaoZhiLiangActivity::class.java)
        intent.putExtra("isXingJian", false)
        startActivityForResult(intent, 2)
    }

    //长按一条
    override fun onItemLongClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long): Boolean {
        UnCeHandler.buzhou = "onItemLongClick"
        if (QuanXian.isShanChuCaiLiaoZhiLiang) {
            var c = context;
            if(c != null){
                val dialog = AlertDialog.Builder(c)
                dialog.setTitle("提示")
                dialog.setMessage("确定要删除     " + cailiaozhiliangs[position].iu_name + "     这条质量问题?")
                dialog.setPositiveButton("确定") { dialog, which ->
                    updatedialog = Myutil.showupdatedialog(activity, "正在删除", false)
                    KP.delete(position)
                }
                dialog.setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
                dialog.show()
            }
        }
        return true
    }

    //下拉刷新
    override fun onHeaderRefresh(view: PullToRefreshView?) {
        UnCeHandler.buzhou = "onHeaderRefresh"
        KP.xiala()
    }

    //上拉加载
    override fun onFooterRefresh(view: PullToRefreshView?) {
        UnCeHandler.buzhou = "onFooterRefresh"
        KP.shangla()
    }

    //上拉加载成功
    override fun shanglaOk() {
        UnCeHandler.buzhou = "shanglaOk"
        Pull.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    //上拉加载失败
    override fun shanglaNo() {
        UnCeHandler.buzhou = "shanglaNo"
        Pull.onFooterRefreshComplete()
    }

    //下拉刷新成功
    override fun xialaOk() {
        UnCeHandler.buzhou = "xialaOk"
        Pull.onHeaderRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    //下拉刷新失败
    override fun xialaNo() {
        UnCeHandler.buzhou = "xialaNo"
        Pull.onHeaderRefreshComplete()
    }

    //搜索成功
    override fun sousuoOk() {
        UnCeHandler.buzhou = "sousuoOk"
        adapter.notifyDataSetChanged()
        updatedialog?.dismiss()
    }

    //搜索失败
    override fun sousuoNo() {
        UnCeHandler.buzhou = "sousuoNo"
        updatedialog?.dismiss()
    }

    //新建材料质量
    fun xingjian() {
        UnCeHandler.buzhou = "xingjian"
        val intent = Intent(context, CaiLiaoZhiLiangActivity::class.java)
        intent.putExtra("isXingJian", true)
        startActivityForResult(intent, 1)
    }

    //删除成功
    override fun deleteOk(position: Int) {
        UnCeHandler.buzhou = "deleteOk"
        Myutil.MyTost("删除成功")
        cailiaozhiliangs.removeAt(position)
        adapter.notifyDataSetChanged()
        updatedialog?.dismiss()
    }

    //删除失败
    override fun deleteNo() {
        UnCeHandler.buzhou = "deleteNo"
        updatedialog?.dismiss()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                1 -> {
                    updatedialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                    KP.sousuo(EtSouSuo.text.toString(), SpHeGe.selectedItem.toString(), TvStartTime.text.toString(), TvEndTime.text.toString())
                }
                2 -> adapter.notifyDataSetChanged()
            }
        }


    }

    companion object {
        var it = CaiLiaoZhiLiangBean()
    }
//    CREATE TABLE IF NOT EXISTS `k_supply_materials_quality`(
//    `id` INT UNSIGNED AUTO_INCREMENT,
//    `gi_id` int(10) unsigned NOT NULL COMMENT 'k_goods_info.id 货品资料ID',
//    `iu_id` int(10) unsigned NOT NULL COMMENT 'k_inventory_supplier.id 供应商数据库ID',
//    `mc_2_id`  INT unsigned NOT NULL COMMENT 'k_material_category2.id 材料类别据库ID',
//    `qi_id` int(10) unsigned NOT NULL COMMENT 'k_corder_quality_issues.id 问题代码数据库ID',
//    `test_time` INT(15) unsigned NOT NULL COMMENT '检验时间',
//    `cailiao_shuliang` INT(10) unsigned NOT NULL COMMENT '材料数量',
//    `choucha_shuliang` INT(10) unsigned NOT NULL COMMENT '抽查数量',
//    `hege_shuliang` INT(10) unsigned NOT NULL COMMENT '合格数量',
//    `quality_decision` VARCHAR(40) NOT NULL COMMENT '质量判定',
//    `process_mode` VARCHAR(40) NOT NULL COMMENT '处理方式',
//    `solution` VARCHAR(100) NOT NULL COMMENT '采购解决方案',
//    PRIMARY KEY ( `id` )
//    )ENGINE=InnoDB DEFAULT CHARSET=utf8;
//    alter table `k_supply_materials_quality` add column `description` varchar(255) NOT NULL COMMENT '质量原由';

}